package com.fer.hr.nmbp.lab2.gui;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import org.apache.ibatis.session.SqlSession;

import com.hr.fer.nmbp.lab2.domain.Room;
import com.hr.fer.nmbp.lab2.domain.TimeSpan;
import com.hr.fer.nmbp.lab2.persistence.ReservationMapper;
import com.toedter.calendar.JDateChooser;

public class CheckReservationsWindow extends JFrame {
	private static final long serialVersionUID = 1345289825483255026L;
	
	private JPanel contentPane;
	private DefaultListModel model;
	private JList listRooms;
	private JDateChooser dtFirst;
	private JDateChooser dtLast;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					CheckReservationsWindow frame = new CheckReservationsWindow();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	private void showClick(){
		model.clear();
		SqlSession session = null;
		session = MainWindow.sessionFactory.openSession();
		
		ReservationMapper reservationMapper = session.getMapper(ReservationMapper.class);
		TimeSpan ts = new TimeSpan(dtFirst.getDate(), dtLast.getDate());
		List<Room> rooms = reservationMapper.selectEmptyRooms(ts);
		
		for(int i=0;i<rooms.size();i++){
			model.add(i, rooms.get(i).getRoomNum());
		}
		
		session.commit();
		
		session.close();
	}
	
	private void closeClick(){
		this.dispose();
	}
	/**
	 * Create the frame.
	 */
	public CheckReservationsWindow() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 259, 326);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblPrviDan = new JLabel("Prvi dan:");
		lblPrviDan.setBounds(10, 26, 61, 14);
		contentPane.add(lblPrviDan);
		
		JLabel lblZadnjiDan = new JLabel("Zadnji dan:");
		lblZadnjiDan.setBounds(10, 51, 61, 14);
		contentPane.add(lblZadnjiDan);
		
		dtFirst = new JDateChooser();
		dtFirst.setBounds(99, 20, 99, 20);
		contentPane.add(dtFirst);
		
		dtLast = new JDateChooser();
		dtLast.setBounds(99, 45, 99, 20);
		contentPane.add(dtLast);
		
		JButton btnOdustani = new JButton("Odustani");
		btnOdustani.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				closeClick();
			}
		});
		btnOdustani.setBounds(10, 94, 89, 23);
		contentPane.add(btnOdustani);
		
		model = new DefaultListModel();
		listRooms = new JList(model);
		listRooms.setBounds(10, 166, 67, 111);
		contentPane.add(listRooms);
		
		JButton btnPretrai = new JButton("Pretraži");
		btnPretrai.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				showClick();
			}
		});
		btnPretrai.setBounds(109, 94, 89, 23);
		contentPane.add(btnPretrai);
		
		JLabel lblPopisSlobodnihSoba = new JLabel("Popis slobodnih soba:");
		lblPopisSlobodnihSoba.setBounds(10, 128, 134, 33);
		contentPane.add(lblPopisSlobodnihSoba);
		
		
	}
}
